/**
 * 
 */
package mlv.jee.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import mlv.jee.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.RootPanel;
import java.util.Hashtable;
import javax.naming.InitialContext;
import javax.naming.Context;
/**
 * @author FiFi
 *
 */
public class Connexion extends FlexTable{
	
	public Connexion(){
		
		// Create a table to layout the form options
	    //FlexTable layout = new FlexTable();
	    this.setCellSpacing(6);
	    this.setWidth("300px");
	    FlexCellFormatter cellFormatter = this.getFlexCellFormatter();

	    // Add a title to the form
	    this.setHTML(0, 0, "Connexion");
	    cellFormatter.setColSpan(0, 0, 2);
	    cellFormatter.setHorizontalAlignment(0, 0,HasHorizontalAlignment.ALIGN_CENTER);

	    // Add some standard form options
	    this.setHTML(1, 0, "Login");
	    final TextBox login= new TextBox();
	    this.setWidget(1, 1, login);
	    this.setHTML(2, 0, "Password");
	    final TextBox password= new TextBox();
	    this.setWidget(2, 1,password);
	    this.setHTML(3, 0, "Connexion");
	    final Button sendButton = new Button("Send");
	    this.setWidget(3, 1, sendButton);
	    
	    // Create some advanced options
	    HorizontalPanel genderPanel = new HorizontalPanel();
	    String[] genderOptions = {"Prive ","Public"};
	    for (int i = 0; i < genderOptions.length; i++) {
	      genderPanel.add(new RadioButton("gender", genderOptions[i]));
	    }
	    Grid advancedOptions = new Grid(2, 2);
	    advancedOptions.setCellSpacing(6);
	    
	    advancedOptions.setHTML(1, 0, "Ordinateur");
	    advancedOptions.setWidget(1, 1, genderPanel);

	    // Add advanced options to form in a disclosure panel
	    DisclosurePanel advancedDisclosure = new DisclosurePanel("Option Avancee");
	    advancedDisclosure.ensureDebugId("cwDisclosurePanel");
	    advancedDisclosure.setContent(advancedOptions);
	    this.setWidget(3, 0, advancedDisclosure);
	    cellFormatter.setColSpan(3, 0, 2);

	    final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
	    
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		
	    
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}

			
		});
		
		final Label errorLabel = new Label();
		final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
		final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
		
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendNameToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendNameToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				String loginToServer = login.getText();
				String passwordToServer = password.getText();
				/* ici */
				


				if (!FieldVerifier.isValidName(loginToServer) & !FieldVerifier.isValidName(passwordToServer)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				sendButton.setEnabled(false);
				textToServerLabel.setText(loginToServer);
				textToServerLabel.setText(passwordToServer);
				serverResponseLabel.setText("");
				greetingService.greetServer(loginToServer,new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Remote Procedure Call - Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								closeButton.setFocus(true);
							}
						});
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		login.addKeyUpHandler(handler);
		password.addKeyUpHandler(handler);
	    
	}

}
